// pages/myPan/settings/address/address.js
import api from '../../../../api/api.js'
import load from '../../../templates/loader/loader.js'

var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressList: [], //地址列表
    loginInfo: [],
    empty: {
      isEmpty: false,
      type: "address",
      text: "没有填写地址"
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var that = this;
    that.loader = load.loader(this, {
      size: 50,
      borderWidth: 3
    });
    //获取用户信息
    api.getUser({
      done: (user) => {
        that.data.loginInfo = user
        that.onLoadData();
      }
    })
  },
  onShow: function () {
    var that = this;
    //获取添加的地址
    that.loader.open()
    wx.getStorage({
      key: 'addAddress',
      success: function (res) {
        that.loader.close()
        var addresList = that.data.addressList;
        if (res.data.addressType == 1) {
          addresList.push(res.data)
        } else {
          addresList.forEach(function (item, i) {
            if (item.id == res.data.id) {
              res.data.default_address = addresList[i].default_address
              addresList[i] = res.data
            }
          })
        }
        //addresList.push(res.data)
        var empty = that.data.empty
        if (addresList.length == 1) {
          res.data.default_address = 1;
        }
        if (addresList.length > 0) {
          empty.isEmpty = false
        } else {
          empty.isEmpty = true
        }
        that.setData({
          addressList: addresList,
          empty: empty
        });
        //push完销毁本地地址
        wx.removeStorage({
          key: 'addAddress'
        })
      },
      fail: function () {
        that.loader.close()
      }
    });
  },
  delAddress: function (e) {
    //删除地址
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定删除？',
      confirmColor: "#e30c26",
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            mask:true
          })
          var delId = e.target.dataset.id;
          var index = e.target.dataset.index;
          var addressList = that.data.addressList; //获取当前地址列表
          var jsonStr = '{"REQ_HEAD":{"SIGN":"null"},"REQ_BODY":{"id":"' + delId + '","userId":"' + that.data.loginInfo.id + '"}}'
          api.delAddressItem({
            method: 'POST',
            data: {
              REQ_MESSAGE: jsonStr
            },
            success: (res) => {
              wx.hideLoading()
              if (res.REP_BODY.RSPCOD == "000000") {
                var empty = that.data.empty
                empty.isEmpty = false
                wx.showToast({
                  title: '已删除',
                  duration: 2000
                })
                addressList.splice(index, 1); //删除当前项
                //当剩下最后一项时设置当前默认地址
                if (addressList.length == 1) {
                  that.setDefaultAddress(addressList[0].id);
                }
                if (addressList.length == 0) {
                  empty.isEmpty = true
                }
                that.setData({
                  addressList: addressList,
                  empty: empty
                }); //更新地址列表
              } else {
                wx.showToast({
                  title: '删除失败！',
                  image: '/image/ico/error-ico.png',
                  duration: 2000
                })
              }
            }
          })
        }
      }
    })

  },
  onLoadData: function () {
    var that = this;
    var jsonStr = '{"REQ_HEAD":{"SIGN":"null"},"REQ_BODY":{"userId":"' + that.data.loginInfo.id + '","id":""}}';
    that.loader.open()
    //获取地址列表
    api.getAddressList({
      method: 'POST',
      data: {
        REQ_MESSAGE: jsonStr
      },
      success: (res) => {
        var empty = that.data.empty
        that.loader.close()
        if (res.rows.length <= 0 || !res.rows) {
          //列表为空
          empty.isEmpty = true
        } else {
          empty.isEmpty = false
          that.setData({
            addressList: res.rows
          });
        }
        that.setData({
          empty: empty
        })
      }
    })
  },
  onSetDefaultAddress: function (e) {
    if (this.data.addressList[e.currentTarget.dataset.index].default_address != 1) {
      this.setDefaultAddress(e.currentTarget.dataset.id);
    }
  },
  setDefaultAddress: function (id) {
    var that = this;
    var addressList = that.data.addressList; //获取列表
    var jsonStr = '{"REQ_HEAD":{"SIGN":"null"},"REQ_BODY":{"userId":"' + that.data.loginInfo.id + '","id":"' + id + '"}}'
    //设置默认地址
    api.setDefaultAddress({
      method: 'POST',
      data: {
        REQ_MESSAGE: jsonStr
      },
      success: (res) => {
        if (res.REP_BODY.RSPCOD == "000000") {
          for (var i in addressList) {
            addressList[i].default_address = 0;
            if (addressList[i].id == id) {
              addressList[i].default_address = 1;
            }
          }
          that.setData({
            addressList: addressList
          }); //更新地址列表
        }

      }
    })
  }
})